package com.qtenv.datasync.service.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.qtenv.datasync.base.Result;
import com.qtenv.datasync.service.entity.DmaFreeze;
import com.qtenv.datasync.service.service.DmaFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * dma冻结数据 前端控制器
 * </p>
 *
 * @author CBL
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/dma/freeze")
public class DmaFreezeController {
    @Autowired
    private DmaFreezeService dmaFreezeService;

    @GetMapping("/getDmaSupplyDataList")
    public Result<?> getDmaSupplyDataList(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        return Result.ok(dmaFreezeService.getDmaSupplyDataList(tenantId, areaId));
    }

    @GetMapping("/getBuildingUsedFlow")
    public Result<?> getBuildingUsedFlow(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId,
                                         @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        return Result.ok(dmaFreezeService.getBuildingUsedFlow(tenantId, areaId, startDate, endDate));
    }

    @GetMapping("/getBuildingHistoryUsedFlow")
    public Result<?> getBuildingHistoryUsedFlow(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        String[] keyArr = {"freezetime", "inputFlow", "buildingFlow", "householdFlow", "buildingProdSaleRatio", "areaLeakRatio", "buildingLeakRatio"};
        return Result.ok(this.transformBuildingHistoryUsedFlowList(dmaFreezeService.getBuildingHistoryUsedFlow(tenantId, areaId), keyArr));
    }

    @GetMapping("/getLeakageAssessmentData")
    public Result<?> getLeakageAssessmentData(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId, @RequestParam("dataMonth") String dataMonth) {
        return Result.ok(dmaFreezeService.getLeakageAssessmentData(tenantId, areaId, dataMonth));
    }

    @GetMapping("/getLeakageCompositionData")
    public Result<?> getLeakageCompositionData(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId, @RequestParam("dataMonth") String dataMonth) {
        return Result.ok(dmaFreezeService.getLeakageCompositionData(tenantId, areaId, dataMonth));
    }

    @GetMapping("/getLeakageApertureData")
    public Result<?> getLeakageApertureData(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId, @RequestParam("dataMonth") String dataMonth) {
        return Result.ok(dmaFreezeService.getLeakageApertureData(tenantId, areaId, dataMonth));
    }

    @GetMapping("/getInvestmentAdviceData")
    public Result<?> getInvestmentAdviceData(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId, @RequestParam("dataMonth") String dataMonth) {
        return Result.ok(dmaFreezeService.getInvestmentAdviceData(tenantId, areaId, dataMonth));
    }

    @GetMapping("/getDmaMinNightFlow")
    public Result<?> getDmaMinNightFlow(@RequestParam("areaId") String areaId, @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
        return Result.ok(dmaFreezeService.getDmaMinNightFlow(areaId, startDate, endDate));
    }

    private Map<String, Object> transformBuildingHistoryUsedFlowList(List<DmaFreeze> dmaFreezeList, String[] keyArr) {
        Map<String, Object> result = MapUtil.newHashMap();

        if (CollectionUtil.isNotEmpty(dmaFreezeList)) {
            result.put("xAxis", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[0])).collect(Collectors.toList()));
            result.put("area", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[1])).collect(Collectors.toList()));
            result.put("building", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[2])).collect(Collectors.toList()));
            result.put("meter", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[3])).collect(Collectors.toList()));
            result.put("buildingProdSaleRatio", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[4])).collect(Collectors.toList()));
            result.put("areaLeakRatio", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[5])).collect(Collectors.toList()));
            result.put("buildingLeakRatio", dmaFreezeList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[6])).collect(Collectors.toList()));
        }

        return result;
    }
}
